package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.FavouriteService;
import cn.itcast.travel.service.RouteService;
import cn.itcast.travel.service.impl.FavouriteServiceImpl;
import cn.itcast.travel.service.impl.RouteServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings("all")
@WebServlet("/route/*")
public class RouteServlet extends BaseServlet{
    RouteService routeService = new RouteServiceImpl();
    FavouriteService favouriteService = new FavouriteServiceImpl();
    public void pageQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RouteService routeService = new RouteServiceImpl();
        String currentPageStr = req.getParameter("currentPage");
        String pageSizeStr = req.getParameter("pageSize");
        String cidStr = req.getParameter("cid");
        String rname = req.getParameter("rname");
        rname=new String(rname.getBytes("iso-8859-1"),"utf-8");
        int currentPage = 0;
        if(currentPageStr != null && currentPageStr.length()>0 && !"null".equals(currentPageStr)){
            currentPage = Integer.parseInt(currentPageStr);
        }else{
            currentPage=1;
        }
        int pageSize = 0;
        if(pageSizeStr != null && pageSizeStr.length()>0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else{
            pageSize = 5;
        }
        int cid =0;
        if(cidStr != null && cidStr.length()>0 && !"null".equals(cidStr)){

            cid = Integer.parseInt(cidStr);
        }
        System.out.println(rname);
        PageBean<Route> routePageBean = routeService.QueryPage(cid, currentPage, pageSize,rname);
        WriteValue(routePageBean, resp);
    }
    public void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        Route route = routeService.findOne(rid);
        WriteValue(route, resp);
    }
    public void  isFavourite (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        User user = (User)req.getSession().getAttribute("user");
        int uid;
        if(user == null){
            uid = 0;
        }else{
            uid=user.getUid();
        }

        boolean flag = favouriteService.isFavourite(rid, uid);
        WriteValue(flag, resp);
    }
    public void addFavourite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        User user = (User)req.getSession().getAttribute("user");
        int uid;
        if(user == null){
            return;
        }else{
            uid = user.getUid();
        }
        favouriteService.addFavourite(rid,uid);
    }
    public void delFavourite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        User user = (User)req.getSession().getAttribute("user");
        int uid;
        if(user == null){
            return;
        }else{
            uid = user.getUid();
            favouriteService.delFavourite(rid, uid);
        }

    }
}
